
var iconv = require('iconv-lite');
const jsdom = require("jsdom");
const fs = require("fs");
const { JSDOM } = jsdom;
const { Script } = require("vm");

var content = fs.readFileSync('colorado_1026_success.html', {encoding:'binary'});
var buf = new Buffer.from(content, 'binary');
var colorado_html = iconv.decode(buf, 'utf-8');
//console.log(colorado_html);

plugins = [];
var options = {
        // referrer: "",
        userAgent: "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
        resources: "usable",    // 假如你想通过<script src="">来执行外部脚本，你需要确保已经加载了它们。为此，请添加选项resources:"usable"
        runScripts: "dangerously",
        url: 'https://www.sos.state.co.us/',
        beforeParse(window) {
            window.document.addEventListener = (a, b, c) => {
                // console.log('a:' + a)
                console.log('b:' + b)
                // console.log('c:' + c)
                return b()
            };
            window.document.fireAnEvent = (a, b, c, d, e) => {
                console.log('fireAnEvent a:' + a)
                // console.log('fireAnEvent b:' + b)
                // console.log('fireAnEvent c:' + c)
                // console.log('fireAnEvent d:' + d)
                // console.log('fireAnEvent e:' + e)
                // return b()
            };
            window.screen = {width: 2560, availHeight: 1400, availLeft: 0, availTop:0, availWidth: 2560,colorDepth: 24, height: 1440, pixelDepth: 24,
                orientation: {
                    angle:0,
                    type: "landscape-primary"
                }};
            window.getjs = () => {
                return window.js
            };
            window.navigator.languages = ["zh-CN", "zh"];
            window.navigator.plugins = plugins
            window.navigator.getBattery = () => {
                return {
                    'charging': true,
                    'chargingTime': 0,
                    'dischargingTime': Infinity,
                    'level': 1,
                }
            };
            window.navigator.mimeTypes = [{"type": "application/360softmgrplugin"}, {"type": "application/aliedit"}, {"type": "application/asx"}, {"type": "application/bd-npyunwebdetect-plugin"}, {"type": "application/cenroll.cenroll.version.1"}, {"type": "application/futuresplash"}, {"type": "application/futuresplash"}, {"type": "application/futuresplash"}, {"type": "application/futuresplash"}, {"type": "application/hwepass2001.installepass2001"}, {"type": "application/hwpta.itrushwpta"}, {"type": "application/hwwdkey.installwdkey"}, {"type": "application/itrusenroll.certenroll.version.1"}, {"type": "application/mozilla-npqihooquicklogin"}, {"type": "application/npalicdo"}, {"type": "application/npalissologin"}, {"type": "application/npqqwebgame"}, {"type": "application/nptxsso"}, {"type": "application/npxf-qqminidl"}, {"type": "application/npxluser_plugin"}, {"type": "application/pdf"}, {"type": "application/pta.itruspta.version.1"}, {"type": "application/qscall-plugin"}]
            window.navigator.connection = {
                'downlink': 10,
                'effectiveType': '4g',
                'rtt': 50,
                'saveData': false
            };
            window.Infinity = Infinity;
            // window.ol = 'Null'
        },
        // virtualConsole: new jsdom.VirtualConsole(),
        cookieJar: new jsdom.CookieJar(),
        pretendToBeVisual: true,
    };

const dom = (new JSDOM(colorado_html, options));
// const dom = (new JSDOM(colorado_html,  {runScripts: "outside-only" }));
const window = dom.window;

// const s = new Script(`
//       window.ol = results.ol;
//     `);
// dom.runVMScript(s);

// var flag = true;
cookie = window.jJ;
console.log("ol: " + cookie)